package com.zing.controller.user;

import com.zing.dto.ShoppingCartDTO;
import com.zing.entity.ShoppingCart;
import com.zing.result.Result;
import com.zing.service.ShoppingCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.ResultMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/shoppingCart")
@Api(tags="用户端购物车相关api")
@Slf4j
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    @PostMapping("/add")
    @ApiOperation("购物车添加商品")
    public Result<Object> add(@RequestBody ShoppingCartDTO shoppingCartDTO){
        log.info("购物车添加商品: {}", shoppingCartDTO);
        shoppingCartService.addShoppingCart(shoppingCartDTO);
        return Result.success();
    }

    @PostMapping("/sub")
    @ApiOperation("购物车减少商品")
    public Result<Object> sub(@RequestBody ShoppingCartDTO shoppingCartDTO){
        log.info("购物车减少商品: {}", shoppingCartDTO);
        shoppingCartService.reduceShoppingCart(shoppingCartDTO);
        return Result.success();
    }

    @GetMapping("/list")
    @ApiOperation("查询购物车列表")
    public Result<List<ShoppingCart>> list(){
        log.info("查询购物车列表");
        List<ShoppingCart> list = shoppingCartService.list();
        return Result.success(list);
    }

    @DeleteMapping("/clean")
    @ApiOperation("清空购物车数据")
    public Result<String> clean(){
        log.info("清空购物车数据");
        shoppingCartService.cleanByUserId();
        return Result.success();
    }
}
